// pages/message/message.js
import {Message} from '../../../models/message'
import {check} from '../../../util/identityCheck'

let messageModel = new Message()

Page({
  data: {
    tabs: ['转店', '求职招聘', '客服'],
    tabIndex: 0,
    data:[],
    showLoading: false,
    haveLogin:false
  },

  onShow () {
    if(!!wx.getStorageSync('userInfo')){
      this.setData({
        haveLogin: true
      })
    }else{
      this.setData({
        haveLogin: false
      })
    }

    messageModel.message({
      type: 1,
      page: 0
    }, res => {
      console.log(res)
    })
  },

  onLoad () {
    if(!check())return
  },

  chooseTab (event) {
    let index =  event.detail.current || event.target.dataset.index || 0
    this.setData({
      tabIndex: index
    })

    messageModel.message({
      type: index,
      page: 0
    }, res => {
      this.setData({
        data: res
      })
    })
  },

  toNewPage (event) {
    let path = event.currentTarget.dataset.path
    let id = event.currentTarget.dataset.id
    wx.navigateTo({
      url: `/pages/${path}/${path}?id=${id}`
    })
  },

  onPullDownRefresh () {

  },

  onReachBottom () {
  },
  onTabItemTap() {
    if (!wx.getStorageSync('userInfo')) {
      wx.navigateTo({
        url: `/pages/login/login`
      })
    }
  }
})
